//
//  MyPictureViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/26.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "MyPictureViewController.h"
#import "ActiveListCell.h"
#import "ActiveDetailViewController.h"
@interface MyPictureViewController ()<UITableViewDataSource, UITableViewDelegate>
{
//    BOOL _isEditing;  // 判断UITableView是否处于编辑状态
    int page;
    NSMutableArray * _dataArray;
}
@property (nonatomic, strong) UITableView * pictureTab;
@property (nonatomic, strong)UIButton *deleteBtn;
@end

@implementation MyPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pictureTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    page = 1;
    _dataArray = @[].mutableCopy;
    [self getMyPictureRequest];
 
     [self customNaviItemTitle:@"我的随手拍" isLeft:YES];
//     [self customTabBarButtonTitle:@"编辑" image:nil target:self action:@selector(rightButtonEvent) isLeft:NO];
    [self.view addSubview:self.pictureTab];
 
    WeakSelf;
    [_pictureTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo (weakSelf.view);
        
    }];

    _pictureTab.estimatedRowHeight = 254.f;
    _pictureTab.rowHeight = UITableViewAutomaticDimension;
    self.pictureTab.delegate = self;
    self.pictureTab.dataSource = self;
    self.pictureTab.tableFooterView = [UIView new];
    [self.pictureTab registerNib:[UINib nibWithNibName:@"ActiveListCell" bundle:nil] forCellReuseIdentifier:@"ActiveListCell"];
    
    _deleteBtn = [[UIButton alloc] init];
    [self.view addSubview:_deleteBtn];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo (weakSelf.view);
        make.height.equalTo (@(50.0f));
        
    }];
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    _deleteBtn.backgroundColor = RGBA(81, 158, 238, 1);
    _deleteBtn.hidden = YES;
    [_deleteBtn addTarget:self action:@selector(onDeleteBtn) forControlEvents:UIControlEventTouchUpInside];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page =1;
        [self getMyPictureRequest];
       
        
    }];
    self.pictureTab.mj_header =header;
        MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
              page++;
              [self getMyPictureRequest];
        }];

        footer.automaticallyRefresh = YES;
     self.pictureTab.mj_footer =footer;
    //    self.bottomTab.mj_footer.hidden = NO;
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _dataArray.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.f;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic = _dataArray[indexPath.section];
    ActiveListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ActiveListCell" forIndexPath:indexPath];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
         [cell.actImage sd_setImageWithURL:ImageUrl(dic[@"cover_pic"]) placeholderImage:ImageName(@"")];
    cell.actImage.contentMode=UIViewContentModeScaleAspectFill;
    cell.actImage.clipsToBounds=YES;
    cell.titleLabel.text = dic[@"title"];
    cell.commentNumber.text = [NSString stringWithFormat:@"%ld",[dic[@"comment_num"] integerValue]];
    cell.lookNumber.text = [NSString stringWithFormat:@"%ld",[dic[@"view_num"] integerValue]];
    cell.timeLabel.text = dic[@"create_time"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * dic = _dataArray[indexPath.section];
    
    
    [self getActDetailsRequestForid:[NSString stringWithFormat:@"%ld",[dic[@"id"] integerValue]]];

}

-(void)getMyPictureRequest{
    
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",page],
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"mac_code":[SHJudgeTools getDeviceId]
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_myPictureList_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray * array = responseObject;
            if (page == 1) {
                if (_dataArray) {
                    [_dataArray removeAllObjects];
                }
                for (NSDictionary * dic in array) {
                    
                    [_dataArray addObject:dic];
                }
            }else {
                for (NSDictionary * dic in array) {
                    
                    [_dataArray addObject:dic];
                }
            }
            
            [_pictureTab reloadData];
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"msg"]);
        }
        
        [_pictureTab.mj_header endRefreshing];
        [_pictureTab.mj_footer endRefreshing];
        NSLog(@"responseObject = %@",responseObject);
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        [_pictureTab.mj_header endRefreshing];
        [_pictureTab.mj_footer endRefreshing];
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
//            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
}

-(void)getActDetailsRequestForid:(NSString *)newsId{
    
    NSDictionary * dic = @{@"news_id":newsId,
                           @"user_id":USER_DEFAULTGet(@"user_id")
                           };
    
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(activity_details_api) andparameters:dic Success:^(id responseObject) {
        
        NSDictionary *  dataDic = responseObject;
        NSLog(@"responseObject = %@",responseObject);
        
        
        
        NSLog(@"responseObject = %@",responseObject);
        
        USER_DEFAULTSet(dataDic[@"pic_url"], @"actDetailsIamgeArray");
        USER_DEFAULTSet(dataDic[@"is_fabulous"], @"act_is_fabulous");
        
        
        if ([dataDic[@"pic_url"] isKindOfClass:[NSArray class]]) {
            NSArray * array = dataDic[@"pic_url"];
            if (array.count==0) {
                
                USER_DEFAULTSet(@"http://a1.qpic.cn/psb?/V147v55d35m6eX/xXdZ2De4s1iE5gYJaikuWyqltDsmFf*LHHZ1ANFPtso!/m/dDABAAAAAAAA&bo=eAB4AAAAAAADByI!&rf=photolist", @"shareImage");
            }else {
                
                USER_DEFAULTSet(dataDic[@"pic_url"][0], @"shareImage");
            }
        }
        NSString * newsId= [NSString stringWithFormat:@"%ld",[dataDic[@"id"] integerValue]];
        USER_DEFAULTSet(newsId,@"shareNewsId");
        USER_DEFAULTSet(@"2",@"shareType");
        USER_DEFAULTSet(dataDic[@"title"], @"shareTitle");
        USER_DEFAULTSet(dataDic[@"content"], @"shareContent");
        USER_DEFAULTSet(dataDic[@"share_url"], @"shareUrl");
        
        
        ActiveDetailViewController * activeDetail = [[ActiveDetailViewController alloc]init];
        activeDetail.actIdStr = newsId;
        activeDetail.dataDic = dataDic;
        activeDetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:activeDetail animated:YES];
        
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
    
}



@end
